# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>
# Contributor: Duong Pham <dthpham@gmail.com>

_realname=numpy
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=2.2.6
pkgrel=1
pkgdesc="Fundamental package for array computing in Python. (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.numpy.org/"
msys2_repository_url="https://github.com/numpy/numpy"
msys2_references=(
  "archlinux: python-numpy"
  "cpe: cpe:/a:numpy:numpy"
  "purl: pkg:pypi/numpy"
)
license=('spdx:BSD-3-Clause')
makedepends=("${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-meson-python"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cython"
             "${MINGW_PACKAGE_PREFIX}-cc")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python-pytest")
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-openblas")
optdepends=("${MINGW_PACKAGE_PREFIX}-python-pytest: testsuite")
options=(!strip)
source=(https://pypi.org/packages/source/${_realname::1}/${_realname}/${_realname}-${pkgver}.tar.gz
        0007-disable-64bit-experimental-warning.patch)
sha256sums=('e29554e2bef54a90aa5cc07da6ce955accb83f21ab5de01a62c8478897b264fd'
            '3506a9c877541045ba06e118ebd37c010b5e165f90264675c04a0f935b294ea4')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd ${_realname}-${pkgver}

  apply_patch_with_msg \
    0007-disable-64bit-experimental-warning.patch
}

build() {
  cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}" && cd "python-build-${MSYSTEM}"

  ${MINGW_PREFIX}/bin/python -m build --wheel --skip-dependency-check --no-isolation \
    -Csetup-args="-Duse-ilp64=false" \
    -Csetup-args="-Dblas=openblas" \
    -Csetup-args="-Dlapack=openblas"
}

package() {
  cd python-build-${MSYSTEM}
  MSYS2_ARG_CONV_EXCL="--prefix=" \
    ${MINGW_PREFIX}/bin/python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl

  find "${pkgdir}${MINGW_PREFIX}" -name '*.dll.a' -delete

  _pyver=$(${MINGW_PREFIX}/bin/python -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")
  install -m755 -d "${pkgdir}${MINGW_PREFIX}/include/python${_pyver}"
  cp -rf "${pkgdir}"${MINGW_PREFIX}/lib/python${_pyver}/site-packages/${_realname}/_core/include/${_realname} "${pkgdir}${MINGW_PREFIX}/include/python${_pyver}/"

  install -Dm644 LICENSE.txt "${pkgdir}"${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE.txt
}
